<?php


class SimpleMethodsComponent extends Object {

/******************************************************************************/

var $Controller = NULL;

var $settings = array();

/******************************************************************************/

function initialize(&$Controller, $settings = array()) {
	$this->Controller = &$Controller;
	$this->settings = (array)$settings;
}

/******************************************************************************/

function toggleField($id = NULL, $field_name = NULL) {
	$ctrl = &$this->Controller;
	if (is_object($ctrl->Model) && $ctrl->Model->isAutomated() && $ctrl->Model->idExists($id)) {
		$status = $ctrl->Model->toggleField($field_name, $id) ? CODE_ACCEPTED : CODE_ERROR;
		$ctrl->redirect($ctrl->referer($ctrl->Model->getIndexPageURL($id), TRUE), $status);
	} else {
		$ctrl->cakeError('error404');
	}
}

/******************************************************************************/

function lock($id = NULL) {
	$ctrl = &$this->Controller;
	if (is_object($ctrl->Model) && $ctrl->Model->isAutomated() && $ctrl->Model->idExists($id)) {
		$status = $ctrl->Model->lock($id) ? CODE_ACCEPTED : CODE_ERROR;
		$ctrl->redirect($ctrl->referer($ctrl->Model->getIndexPageURL($id), TRUE), $status);
	} else {
		$ctrl->cakeError('error404');
	}
}

function unlock($id = NULL) {
	$ctrl = &$this->Controller;
	if (is_object($ctrl->Model) && $ctrl->Model->isAutomated() && $ctrl->Model->idExists($id)) {
		$status = $ctrl->Model->unlock($id) ? CODE_ACCEPTED : CODE_ERROR;
		$ctrl->redirect($ctrl->referer($ctrl->Model->getIndexPageURL($id), TRUE), $status);
	} else {
		$ctrl->cakeError('error404');
	}
}

function toggleLocked($id = NULL) {
	$ctrl = &$this->Controller;
	if (is_object($ctrl->Model) && $ctrl->Model->isAutomated() && $ctrl->Model->idExists($id)) {
		$status = $ctrl->Model->toggleLocked($id) ? CODE_ACCEPTED : CODE_ERROR;
		$ctrl->redirect($ctrl->referer($ctrl->Model->getIndexPageURL($id), TRUE), $status);
	} else {
		$ctrl->cakeError('error404');
	}
}

/******************************************************************************/

function hide($id = NULL) {
	$ctrl = &$this->Controller;
	if (is_object($ctrl->Model) && $ctrl->Model->isAutomated() && $ctrl->Model->idExists($id)) {
		$status = $ctrl->Model->hide($id) ? CODE_ACCEPTED : CODE_ERROR;
		$ctrl->redirect($ctrl->referer($ctrl->Model->getIndexPageURL($id), TRUE), $status);
	} else {
		$ctrl->cakeError('error404');
	}
}

function show($id = NULL) {
	$ctrl = &$this->Controller;
	if (is_object($ctrl->Model) && $ctrl->Model->isAutomated() && $ctrl->Model->idExists($id)) {
		$status = $ctrl->Model->show($id) ? CODE_ACCEPTED : CODE_ERROR;
		$ctrl->redirect($ctrl->referer($ctrl->Model->getIndexPageURL($id), TRUE), $status);
	} else {
		$ctrl->cakeError('error404');
	}
}

function toggleHidden($id = NULL) {
	$ctrl = &$this->Controller;
	if (is_object($ctrl->Model) && $ctrl->Model->isAutomated() && $ctrl->Model->idExists($id)) {
		$status = $ctrl->Model->toggleHidden($id) ? CODE_ACCEPTED : CODE_ERROR;
		$ctrl->redirect($ctrl->referer($ctrl->Model->getIndexPageURL($id), TRUE), $status);
	} else {
		$ctrl->cakeError('error404');
	}
}

/******************************************************************************/

function disable($id = NULL) {
	$ctrl = &$this->Controller;
	if (is_object($ctrl->Model) && $ctrl->Model->isAutomated() && $ctrl->Model->idExists($id)) {
		$status = $ctrl->Model->disable($id) ? CODE_ACCEPTED : CODE_ERROR;
		$ctrl->redirect($ctrl->referer($ctrl->Model->getIndexPageURL($id), TRUE), $status);
	} else {
		$ctrl->cakeError('error404');
	}
}

function enable($id = NULL) {
	$ctrl = &$this->Controller;
	if (is_object($ctrl->Model) && $ctrl->Model->isAutomated() && $ctrl->Model->idExists($id)) {
		$status = $ctrl->Model->enable($id) ? CODE_ACCEPTED : CODE_ERROR;
		$ctrl->redirect($ctrl->referer($ctrl->Model->getIndexPageURL($id), TRUE), $status);
	} else {
		$ctrl->cakeError('error404');
	}
}

function toggleDisabled($id = NULL) {
	$ctrl = &$this->Controller;
	if (is_object($ctrl->Model) && $ctrl->Model->isAutomated() && $ctrl->Model->idExists($id)) {
		$status = $ctrl->Model->toggleDisabled($id) ? CODE_ACCEPTED : CODE_ERROR;
		$ctrl->redirect($ctrl->referer($ctrl->Model->getIndexPageURL($id), TRUE), $status);
	} else {
		$ctrl->cakeError('error404');
	}
}

/******************************************************************************/

function erase($id = NULL) {
	$ctrl = &$this->Controller;
	if (is_object($ctrl->Model) && $ctrl->Model->isAutomated() && $ctrl->Model->idExists($id)) {
		$status = $ctrl->Model->erase($id) ? CODE_ACCEPTED : CODE_ERROR;
		$ctrl->redirect($ctrl->referer($ctrl->Model->getIndexPageURL($id), TRUE), $status);
	} else {
		$ctrl->cakeError('error404');
	}
}

function unerase($id = NULL) {
	$ctrl = &$this->Controller;
	if (is_object($ctrl->Model) && $ctrl->Model->isAutomated() && $ctrl->Model->idExists($id)) {
		$status = $ctrl->Model->unerase($id) ? CODE_ACCEPTED : CODE_ERROR;
		$ctrl->redirect($ctrl->referer($ctrl->Model->getIndexPageURL($id), TRUE), $status);
	} else {
		$ctrl->cakeError('error404');
	}
}

function toggleDeleted($id = NULL) {
	$ctrl = &$this->Controller;
	if (is_object($ctrl->Model) && $ctrl->Model->isAutomated() && $ctrl->Model->idExists($id)) {
		$status = $ctrl->Model->toggleDeleted($id) ? CODE_ACCEPTED : CODE_ERROR;
		$ctrl->redirect($ctrl->referer($ctrl->Model->getIndexPageURL($id), TRUE), $status);
	} else {
		$ctrl->cakeError('error404');
	}
}

/******************************************************************************/

function first($id = NULL) {
	$ctrl = &$this->Controller;
	if (is_object($ctrl->Model) && $ctrl->Model->isAutomated() && $ctrl->Model->idExists($id)) {
		$status = $ctrl->Model->first($id) ? CODE_ACCEPTED : CODE_ERROR;
		$ctrl->redirect($ctrl->referer($ctrl->Model->getIndexPageURL($id), TRUE), $status);
	} else {
		$ctrl->cakeError('error404');
	}
}

function top($id = NULL) {
	$ctrl = &$this->Controller;
	if (is_object($ctrl->Model) && $ctrl->Model->isAutomated() && $ctrl->Model->idExists($id)) {
		$status = $ctrl->Model->top($id) ? CODE_ACCEPTED : CODE_ERROR;
		$ctrl->redirect($ctrl->referer($ctrl->Model->getIndexPageURL($id), TRUE), $status);
	} else {
		$ctrl->cakeError('error404');
	}
}

function up($id = NULL, $number = 1) {
	$ctrl = &$this->Controller;
	if (is_object($ctrl->Model) && $ctrl->Model->isAutomated() && $ctrl->Model->idExists($id)) {
		$status = $ctrl->Model->up($id, $number) ? CODE_ACCEPTED : CODE_ERROR;
		$ctrl->redirect($ctrl->referer($ctrl->Model->getIndexPageURL($id), TRUE), $status);
	} else {
		$ctrl->cakeError('error404');
	}
}

function down($id = NULL, $number = 1) {
	$ctrl = &$this->Controller;
	if (is_object($ctrl->Model) && $ctrl->Model->isAutomated() && $ctrl->Model->idExists($id)) {
		$status = $ctrl->Model->down($id, $number) ? CODE_ACCEPTED : CODE_ERROR;
		$ctrl->redirect($ctrl->referer($ctrl->Model->getIndexPageURL($id), TRUE), $status);
	} else {
		$ctrl->cakeError('error404');
	}
}

function bottom($id = NULL) {
	$ctrl = &$this->Controller;
	if (is_object($ctrl->Model) && $ctrl->Model->isAutomated() && $ctrl->Model->idExists($id)) {
		$status = $ctrl->Model->bottom($id) ? CODE_ACCEPTED : CODE_ERROR;
		$ctrl->redirect($ctrl->referer($ctrl->Model->getIndexPageURL($id), TRUE), $status);
	} else {
		$ctrl->cakeError('error404');
	}
}

function last($id = NULL) {
	$ctrl = &$this->Controller;
	if (is_object($ctrl->Model) && $ctrl->Model->isAutomated() && $ctrl->Model->idExists($id)) {
		$status = $ctrl->Model->last($id) ? CODE_ACCEPTED : CODE_ERROR;
		$ctrl->redirect($ctrl->referer($ctrl->Model->getIndexPageURL($id), TRUE), $status);
	} else {
		$ctrl->cakeError('error404');
	}
}

/******************************************************************************/

function before($id = NULL, $neighbor_id = NULL) {
	$ctrl = &$this->Controller;
	if (is_object($ctrl->Model) && $ctrl->Model->isAutomated() && $ctrl->Model->idExists($id) && $ctrl->Model->idExists($neighbor_id)) {
		$status = $ctrl->Model->before($id, $neighbor_id) ? CODE_ACCEPTED : CODE_ERROR;
		$ctrl->redirect($ctrl->referer($ctrl->Model->getIndexPageURL($id), TRUE), $status);
	} else {
		$ctrl->cakeError('error404');
	}
}

function after($id = NULL, $neighbor_id = NULL) {
	$ctrl = &$this->Controller;
	if (is_object($ctrl->Model) && $ctrl->Model->isAutomated() && $ctrl->Model->idExists($id) && $ctrl->Model->idExists($neighbor_id)) {
		$status = $ctrl->Model->after($id, $neighbor_id) ? CODE_ACCEPTED : CODE_ERROR;
		$ctrl->redirect($ctrl->referer($ctrl->Model->getIndexPageURL($id), TRUE), $status);
	} else {
		$ctrl->cakeError('error404');
	}
}

/******************************************************************************/

function move($id = NULL, $parent_id = NULL) {
	$ctrl = &$this->Controller;
	if (is_object($ctrl->Model) && $ctrl->Model->isAutomated() && $ctrl->Model->isTree() && $ctrl->Model->idExists($id) && (empty($parent_id) || $ctrl->Model->idExists($parent_id))) {
		$status = $ctrl->Model->move($id, $parent_id) ? CODE_ACCEPTED : CODE_ERROR;
		$ctrl->redirect($ctrl->referer($ctrl->Model->getIndexPageURL($id), TRUE), $status);
	} else {
		$ctrl->cakeError('error404');
	}
}

/******************************************************************************/

function moveBefore($id = NULL, $parent_id = NULL, $neighbor_id = NULL) {
	$ctrl = &$this->Controller;
	if (is_object($ctrl->Model) && $ctrl->Model->isAutomated() && $ctrl->Model->isTree() && $ctrl->Model->idExists($id) && (empty($parent_id) || $ctrl->Model->idExists($parent_id)) && $ctrl->Model->idExists($neighbor_id)) {
		$status = $ctrl->Model->moveBefore($id, $parent_id, $neighbor_id) ? CODE_ACCEPTED : CODE_ERROR;
		$ctrl->redirect($ctrl->referer($ctrl->Model->getIndexPageURL($id), TRUE), $status);
	} else {
		$ctrl->cakeError('error404');
	}
}

function moveAfter($id = NULL, $parent_id = NULL, $neighbor_id = NULL) {
	$ctrl = &$this->Controller;
	if (is_object($ctrl->Model) && $ctrl->Model->isAutomated() && $ctrl->Model->isTree() && $ctrl->Model->idExists($id) && (empty($parent_id) || $ctrl->Model->idExists($parent_id)) && $ctrl->Model->idExists($neighbor_id)) {
		$status = $ctrl->Model->moveAfter($id, $parent_id, $neighbor_id) ? CODE_ACCEPTED : CODE_ERROR;
		$ctrl->redirect($ctrl->referer($ctrl->Model->getIndexPageURL($id), TRUE), $status);
	} else {
		$ctrl->cakeError('error404');
	}
}

/******************************************************************************/

function dropBefore($id = NULL, $neighbor_id = NULL) {
	$ctrl = &$this->Controller;
	if (is_object($ctrl->Model) && $ctrl->Model->isAutomated() && $ctrl->Model->idExists($id) && $ctrl->Model->idExists($neighbor_id)) {
		$status = $ctrl->Model->dropBefore($id, $neighbor_id) ? CODE_ACCEPTED : CODE_ERROR;
		$ctrl->redirect($ctrl->referer($ctrl->Model->getIndexPageURL($id), TRUE), $status);
	} else {
		$ctrl->cakeError('error404');
	}
}

function dropAfter($id = NULL, $neighbor_id = NULL) {
	$ctrl = &$this->Controller;
	if (is_object($ctrl->Model) && $ctrl->Model->isAutomated() && $ctrl->Model->idExists($id) && $ctrl->Model->idExists($neighbor_id)) {
		$status = $ctrl->Model->dropAfter($id, $neighbor_id) ? CODE_ACCEPTED : CODE_ERROR;
		$ctrl->redirect($ctrl->referer($ctrl->Model->getIndexPageURL($id), TRUE), $status);
	} else {
		$ctrl->cakeError('error404');
	}
}

/******************************************************************************/

function back($id = NULL, $status = NULL) {
	$ctrl = &$this->Controller;
	if (is_object($ctrl->Model) && $ctrl->Model->isAutomated()) {
		if ($ctrl->Model->idExists($id)) {
			$ctrl->redirect($ctrl->Model->getIndexPageURL($id), $status);
		} else {
			$ctrl->redirect(array('controller' => Inflector::tableize($ctrl->Model->name), 'action' => 'index'), $status);
		}
	} else {
		$ctrl->cakeError('error404');
	}
}

function rubricBack($rubric_id = NULL, $status = NULL) { // deprecated
	$ctrl = &$this->Controller;
	if (is_object($ctrl->RubricModel) && $ctrl->RubricModel->isAutomated()) {
		if ($ctrl->RubricModel->idExists($rubric_id)) {
			$ctrl->redirect($ctrl->RubricModel->getIndexURL($rubric_id), $status);
		} else {
			$ctrl->redirect(array('controller' => Inflector::tableize($ctrl->RubricModel->name), 'action' => 'index'), $status);
		}
	} else {
		$ctrl->cakeError('error404');
	}
}

/******************************************************************************/

function recover() {
	$ctrl = &$this->Controller;
	if (is_object($ctrl->Model) && $ctrl->Model->isAutomated() && $ctrl->Model->isTree()) {
		$status = $ctrl->Model->recover() ? CODE_ACCEPTED : CODE_ERROR;
		$ctrl->redirect($ctrl->referer($ctrl->Model->getIndexURL(), TRUE), $status);
	} else {
		$ctrl->cakeError('error404');
	}
}

function reorder() {
	$ctrl = &$this->Controller;
	if (is_object($ctrl->Model) && $ctrl->Model->isAutomated() && $ctrl->Model->isTree()) {
		$status = $ctrl->Model->reorder() ? CODE_ACCEPTED : CODE_ERROR;
		$ctrl->redirect($ctrl->referer($ctrl->Model->getIndexURL(), TRUE), $status);
	} else {
		$ctrl->cakeError('error404');
	}
}

function verify() {
	$ctrl = &$this->Controller;
	if (is_object($ctrl->Model) && $ctrl->Model->isAutomated() && $ctrl->Model->isTree()) {
		$status = (bool)$ctrl->Model->verify() ? CODE_ACCEPTED : CODE_ERROR;
		$ctrl->redirect($ctrl->referer($ctrl->Model->getIndexURL(), TRUE), $status);
	} else {
		$ctrl->cakeError('error404');
	}
}

/******************************************************************************/

}


?>